/**
 * 
 */
var role = {
	id : 'role-panel',
	title : '角色视图',
	layout : 'border',
	defaults : {
		
		bodyStyle : 'padding:15px'
	},
	items : [  {
		tbar: [{xtype:'button',width:50,text:'返回',iconCls: 'return',handler:returnToRoleList}],
		region : 'center',
		border : false,
		autoScroll:true,
		contentEl : 'roleCanvas'
	} ]
};

function returnToRoleList(){
	
	  Ext.getCmp('content-panel').layout.setActiveItem('role-list-panel');
	  showRoleList();
}

var rolepermissionsStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showRolepermissions.action?method=showRolepermissions',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : "RolepermissionsStore",
		totalProperty : "totalRolepermissionsStore",
		id:"id"
    }, [ {
		name : 'id'
	}, {
		name : 'permissionid'
	},{
		name : 'roleid'
	}])
});
function showRoleContent(record) {
	
    cid=record.get('id');
    rolepermissionsStore.load({ 
    	params : {
			cid : cid
		},
		callback: function(r, options, success) { 
			if (success == false) {  
				var canvas=document.getElementById('roleCanvas');
				var ctx=canvas.getContext('2d');
				drawRoleModel(300,200,ctx,rolepermissionsStore,record,canvas);	
				//alert("加载数据失败，无对应数据或者系统出现异常！"); 
			}else{ 
				var canvas=document.getElementById('roleCanvas');
				var ctx=canvas.getContext('2d');
				drawRoleModel(300,200,ctx,rolepermissionsStore,record,canvas);				
			}
		}
		});
   }
function drawRoleModel(startx,starty,ctx,rolepermissionsStore,record,canvas){
	 ctx.restore();
	 ctx.clearRect(0, 0, 800, 800);
	 ctx.save();
	 ctx.translate(startx, starty);
	 var rAmount=rolepermissionsStore.getCount();
	
	 var gradient = ctx.createLinearGradient(0, 0, 0, 50);
	 gradient.addColorStop(0, 'rgb(154, 205, 50)');
	 gradient.addColorStop(1, 'rgb(255,255,240)');
	 ctx.fillStyle = gradient;
	 ctx.fillRect( 0, 0, 100, 50); //渐变的背景色
	 
	 ctx.strokeStyle = '#000000'; 
	 ctx.beginPath(); 
	 ctx.lineWidth = 1;
	 ctx.rect(0.5,0.5, 100, 100);
	 ctx.stroke();              //黑边框
	 
	 var processImage=new Image();
	 processImage.onload=function(){
	 ctx.drawImage(processImage,10,12,16,16);
	 };
	 processImage.src='images/modelUnit/function/process.gif';
	 
	 ctx.fillStyle ="black";
	 ctx.font = "10pt 宋体";
	 ctx.fillText(record.get('cnName'),28,16,170); //标题栏
	 
	 ctx.beginPath();
	 ctx.moveTo(0,20);
	 ctx.lineTo(100,20);
	 ctx.stroke();   
	 ctx.beginPath();
	 ctx.moveTo(0.5,20.5);
	 ctx.lineTo(200.5,20.5);
	 ctx.stroke();        //画分割线
	 
	
	 var functionImg=new Image();
	 functionImg.onload=function(){
     for (var i=0;i<rAmount;i++){
    	    x=parseInt(-150*Math.cos(i*Math.PI*2/rAmount));
    	    if (x>100) x=1.2*x;
    	    y=parseInt(-150*Math.sin(i*Math.PI*2/rAmount));
    	     gradient = ctx.createLinearGradient(x, y, 0, 50);
    		 gradient.addColorStop(0, 'rgb(230, 230, 250)');
    		 gradient.addColorStop(1, 'rgb(255,255,240)');
    		 ctx.fillStyle = gradient;
    		 ctx.fillRect( x, y, 100, 50); //渐变的背景色
    		
    		 ctx.lineWidth = 1;
    		 ctx.strokeStyle = '#000000'; 
    		 ctx.beginPath(); 
    		
    		 ctx.rect(x-0.5,y-0.5, 100, 50);
    		 ctx.stroke();              //黑边框
    		 
			ctx.drawImage(functionImg,x+10,12+y,16,16);
			var record=rolepermissionsStore.getAt(i);	
			ctx.fillStyle ="black";
			ctx.font = "10pt Arial ";
			ctx.fillText(record.get('cnName'),x+30,y+25,70);
			ctx.fillText('['+record.get('enName')+']',x,y+45,100);
			drawLine(x+50,y+25,50,25,ctx,100,50);
		 }
        canvas.onclick = onRoleMouseClick;     
	 };
	 functionImg.src='images/modelUnit/function/function_point.gif';
	 }
function onRoleMouseClick(event) {
	 var fAmount=rolepermissionsStore.getCount();
	 var i=0;
	 var x=event.layerX-300;
	 var y=event.layerY-200;
	 if (x/1.2>100) x=x/1.2;
	 while (i<fAmount){
     
     if (x>-150*Math.cos(i*Math.PI*2/fAmount)
    	 && x<-150*Math.cos(i*Math.PI*2/fAmount)+100
         && y>-150*Math.sin(i*Math.PI*2/fAmount)
         && y<-150*Math.sin(i*Math.PI*2/fAmount)+50)
    {
    	
 	       Ext.getCmp('content-panel').layout.setActiveItem('functionpoint-panel');
 	       showFunctionContent(functionStore.getAt(i).get('id'),functionStore.getAt(i).get('cnName'),processid,pText);
	       break;
     }
      i++;
	 }
}  


